<?php
/**
 +--------------------------------
 * RefundAction.class.php （用户退款）
 * date:2013.12.16
 * @author tanbin
 +--------------------------------
 *
 */
class RefundAction extends BaseAction {
	protected $money,$element;
	function _initialize() {
		parent::_initialize ();		
		$this->dao = M('UserRefund');
		import ( 'Com.Money' );
		$this->money = new Money ();
		
		$this->element = "ORDER_BAK,ADMIN_BV";
	}
	
	/*
	 * 退款记录列表
	 * */
	function index(){
		$where = array();
		if($_REQUEST['moneytype']){
			$where['moneytype'] = $_REQUEST['moneytype'];
		}
		$where['element'] = array('in',$this->element);
		$refundtype=M("SystemConfig")->field('value,title')->where($where)->select();
		$refundtype = self::_arr2to1($refundtype,'value');
		$refundtype = implode(',', $refundtype);
		$where['refundtype'] = array('IN',$refundtype);
		$where = parent::create_where($where);
		$count = $this->dao->where($where)->count();
		if($count){
			$page=new Page($count,$this->listrows);
			$list = $this->dao->where($where)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
			$this->assign('list',$list);
		}
		$this->assign("totalCount",$count);
		$this->assign('where',$where);
		$this->display();
	}	
	
	
	
	/*
	 * 退款申请操作
	* */
	function edit(){
		//获取退款类型
		$where['element'] = array('in',$this->element);
		$refundtype=M("SystemConfig")->field('value,title')->where($where)->select();
		
		if($_REQUEST['id']){
			$info = $this->dao->find($_REQUEST['id']);
			$this->assign('info',$info);
		}else{
			$inf = array();
			$inf['userid'] = $_REQUEST['userid'];
			$inf['username'] = $_REQUEST['uname'];
			$inf['sn'] = $_REQUEST['sn'];
			$inf['price'] = $_REQUEST['prices'];
			$inf['orderid'] = $_REQUEST['orderid'];
			$inf['key'] = $_REQUEST['key'];
			$this->assign('inf',$inf);
		}
		$this->assign('refundtype',$refundtype);
		$this->display();
	}
	
	/*
	 * 保存退款申请信息
	* */
	function save(){
		$data = $_REQUEST['data'];
		$data['userid'] = $_REQUEST['user_id'];
		$data['username'] = $_REQUEST['user_username'];
		$data['aid'] = $this->userid;
		$data['money'] = $data['money']*100;
		
		if($_REQUEST['key'] == 'ordref'){ //订单退款
			M('Order')->where(array('id'=>$_REQUEST['orderid']))->setField('pay_status', 3);
// 			echo M('Order')->getLastSql();
// 			exit;
		}
		if($data['id']){
			$data['mtime'] = time();
			$reid = $this->dao->save($data);
			if($reid>=0){
				$this->_ajm(200,'编辑成功！',1);
			}else{
				$this->_ajm(300,'编辑失败！');
			}
		}else{
			$data['ctime'] = time();
			$reid = $this->dao->add($data);
			if($reid){				
				//********** begin bv by tb 单次采购蛮 1.5w 赠送 100BV *****//
				if($_REQUEST['key'] = 'givebv'){
					$where = array();
					$where['element'] = 'givebv';
					$where['order_id'] = $_REQUEST['orderid'];
					$data_or['value'] = 0;
					$stat = M('OrderExt')->where($where)->save($data_or);
					if ($stat === FALSE) {
						$this->_ajm(300,'已赠送BV，但备注信息写入失败，请把该错误信息及订单号，发给技术部修改状态！');
					}
				}
				//********** begin bv by tb 单次采购蛮 1.5w 赠送 100BV *****//
				
				$this->_ajm(200,'添加成功！','Refund/index');
			}else{
				$this->_ajm(300,'添加失败！');
			}
		}
		
	}
	
	/*
	 *	审核退款操作
	* */
	function audit(){
		$authstatus = $_REQUEST['authstatus'];
		if($authstatus){
			$id = $_REQUEST['id'];
			$info = $this->dao->find($id);
			if(empty($info)){
				$this->_ajm(300,'数据不存在！');
				exit;
			}
			$data['id'] = $id;
			$data['authstatus'] = $authstatus;
			
			switch ($authstatus){
				//核查属实，等待退款
				case 1 :
					$msg = '核查属实，等待退款';
					break;		
				//核查属实，退款完成
				case 2 :
					//退款操作
					if ($info['money']) {					
						//获取退款类型
						$where['value'] = $info['refundtype'];
						$sysconfig=M("SystemConfig")->field('title,element')->where($where)->find();
						$urs = $this->money->recharge ( $info['userid'], $info['moneytype'], $info['money']/100, array (
								'action_userid' => $this->userid,
								'cip' => get_client_ip (),
								'bzvu' => $sysconfig['title'],
								'changtype' => $sysconfig['element'],
						) );
						
						$msg = '核查属实，退款完成';
						
					}
					break;
				//核查不属实
				case 3 :
					$msg = '核查不属实';
					break;
			}
			
			$content = unserialize ( $info ['content'] );
			$i=0;
			if($content){
				foreach ($content as $vo){
					if($vo['authstatus'] == $authstatus){
						$i++;
					}			
				}
			}
			if($i<1){
				$content[] = array('authuid'=>$this->userid,'authstatus'=>$authstatus,'time'=>time(),'msg'=>$msg);
				$data['content']=serialize($content);
			}
			
			$reid = $this->dao->save($data);
			
			if($reid){
				$this->_ajm(200,'操作成功！',0,U( 'Money/refund' ));
			}else{
				$this->_ajm(300,'操作失败！');
			}
			
		}else{
			$this->_ajm(300,'请勾选您的审核结果！');
		}
				
	}
	
	function show(){
		if($_REQUEST['id']){
			//获取退款类型
			$where['element'] = array('in',$this->element);
			$refundtype=M("SystemConfig")->field('value,title')->where($where)->select();
			$info = $this->dao->find($_REQUEST['id']);
			$this->assign('info',$info);
			$this->assign('refundtype',$refundtype);
			$this->display();
		}else{
			$this->_ajm(300,'信息不存在！');
		}
		$this->display();
	}
	
	/**
	 * 退cny
	 * **/
	function cnysave(){
		$info = $_REQUEST['data'];
		$info['userid'] = $_REQUEST['user_id'];
		$handeltype = $_REQUEST['handeltype'];
		if(!$info['userid'] || !$info['money'] || !$info['remark'] || !$handeltype){
			$this->_ajm(300,'信息填写错误,或者信息不完整！');exit;
		}
		$oper = '';
		if($handeltype == 'minus'){
			$oper = '-';
		}
		
		$urs = $this->money->recharge ( $info['userid'], $info['moneytype'], $info['money'], array (
				'action_userid' => $this->userid,
				'cip' => get_client_ip (),
				'bzvu' => $info['remark'],
				'changtype' => 'ORDER_BAK',
				'oper' => $oper
		),'','91YUNGOU' );
		
		if ($urs!==false) {
				$this->_ajm(200,'操作成功!' );
		}else{
				$this->_ajm(300,'操作失败!');
		}
	}
	
	
}
	
